////////////////////////////////////////////////////////////////////////////////
//                                                                            //
// The MIT License (MIT)                                                      //
//                                                                            //
// Copyright (c) 2013 Charles Teague                                          //
//                                                                            //
// Permission is hereby granted, free of charge, to any person obtaining a    //
// copy of this software and associated documentation files (the "Software"), //
// to deal in the Software without restriction, including without limitation  //
// the rights to use, copy, modify, merge, publish, distribute, sublicense,   //
// and/or sell copies of the Software, and to permit persons to whom the      //
// Software is furnished to do so, subject to the following conditions:       //
//                                                                            //
// The above copyright notice and this permission notice shall be included in //
// all copies or substantial portions of the Software.                        //
//                                                                            //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR //
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   //
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    //
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER //
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING    //
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER        //
// DEALINGS IN THE SOFTWARE.                                                  //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using RPSDAL;
using RPSDAL.CollectionClasses;
using RPSDAL.EntityClasses;
using RPSDAL.FactoryClasses;
using RPSDAL.DaoClasses;
using RPSDAL.RelationClasses;
using RPSDAL.ValidatorClasses;
using RPSDAL.TypedViewClasses;
using RPSDAL.StoredProcedureCallerClasses;
using RPSDAL.HelperClasses;

using SD.LLBLGen.Pro.ORMSupportClasses;

public partial class Role : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Parameters.LoggedUser != -1)
            {
                UserEntity eUser = new UserEntity(Parameters.LoggedUser);
                RoleEntity eRole = new RoleEntity(eUser.RoleId);

                if (!eRole.HasEditUserGroup)
                {
                    Parameters.LoggedUser = -1;
                    Response.Redirect("Default.aspx");
                    return;
                }
            }


            lblSteluta.Visible = false;
            lblGrupExist.Visible = false;
            if (Request.QueryString["id"] != null)
            {
                RoleEntity eRole = new RoleEntity(Convert.ToInt32(Global.DecryptString(Request.QueryString["id"].ToString())));

                txtName.Text = eRole.Name;
                ckViewContact.Checked = eRole.HasViewContact;
                ckAssignCall.Checked = eRole.HasAssignCall;
                ckAssignLetters.Checked = eRole.HasAssignLetters;
                ckConfigureCallFinishedPage.Checked = eRole.HasConfigureCallFinishedPage;
                ckConfigureContactInterface.Checked = eRole.HasConfigureContactInterface;
                ckEditBusinesRule.Checked = eRole.HasEditBusinessRules;
                ckEditContact.Checked = eRole.HasEditContact;
                ckEditContactGroup.Checked = eRole.HasEditContactGroup;
                ckEditContactType.Checked = eRole.HasEditContactType;
                ckEditLetterTemplate.Checked = eRole.HasEditTemplate;
                ckEditUser.Checked = eRole.HasEditUser;
                ckEditUserGroup.Checked = eRole.HasEditUserGroup;
                ckSetNumberOfCallsPerWeek.Checked = eRole.HasNumberCallsPerWeek;
                ckSummaryReport.Checked = eRole.HasSummaryReport;
                ckViewBusinessRules.Checked = eRole.HasViewBusinessRules;
                ckViewContact.Checked = eRole.HasViewContact;
                ckViewContactGroup.Checked = eRole.HasViewContactGroup;
                ckViewLetterTemplate.Checked = eRole.HasViewTemplate;
                ckViewAvailableToCallUsers.Checked = eRole.HasViewUsersAvailableToCall;
                ckUserLog.Checked = eRole.HasUserLog;
                ckWelcomeTemplate.Checked = eRole.HasWelcomeTemplate;
                ckMailSettings.Checked = eRole.HasMailSetting;
            }

        }
    }
    protected void Save(object sender, EventArgs e)
    {
        try
        {
            Global.UserLogSave(Parameters.LoggedUser, "Save Group Role Privileges");
            lblSteluta.Visible = false;
            lblGrupExist.Visible = false;
            RoleEntity eRole = new RoleEntity();
            if (Page.IsPostBack)
            {
                eRole = new RoleEntity(Convert.ToInt32(Global.DecryptString(Request.QueryString["id"].ToString())));
            }

            int index = 0;
            if (eRole.IsNew || eRole.Name.ToUpper() != txtName.Text.ToUpper())
            {
                RoleCollection cRoles = new RoleCollection();

                IPredicateExpression filter = new PredicateExpression();
                filter.Add(RoleFields.Name == txtName.Text);
                index = cRoles.GetDbCount(filter);
            }

            eRole.Name = txtName.Text;           
            eRole.HasViewContact = ckViewContact.Checked;
            eRole.HasAssignCall = ckAssignCall.Checked;
           // eRole.HasAssignCallChanged = ckass
            eRole.HasAssignLetters = ckAssignLetters.Checked;
            //eRole.HasAssignLettersChanged = ckAssignLetters;
            eRole.HasConfigureCallFinishedPage = ckConfigureCallFinishedPage.Checked;
            //eRole.HasConfigureCallFinishedPageChanged = ck
            eRole.HasConfigureContactInterface = ckConfigureContactInterface.Checked;
            //eRole.HasConfigureContactInterfaceChanged = ckConfigureContactInterface
            eRole.HasEditBusinessRules = ckEditBusinesRule.Checked;
            //eRole.HasEditBusinessRulesChanged = ckEditBusinesRule
            eRole.HasEditContact = ckEditContact.Checked;
            //eRole.HasEditContactChanged = ckEditContact
            eRole.HasEditContactGroup = ckEditContactGroup.Checked;
            //eRole.HasEditContactGroupChanged = ckEditContactGroup
            eRole.HasEditContactType = ckEditContactType.Checked;
            //eRole.HasEditContactTypeChanged = ckEditContactType
            eRole.HasEditTemplate = ckEditLetterTemplate.Checked;
            //eRole.HasEditLetterTemplateChanged = ckEditLetterTemplate
            eRole.HasEditUser = ckEditUser.Checked;
            //eRole.HasEditUserChanged = ckEditUser
            eRole.HasEditUserGroup = ckEditUserGroup.Checked;
            //eRole.HasEditUserGroupChanged
            eRole.HasNumberCallsPerWeek = ckSetNumberOfCallsPerWeek.Checked;
            eRole.HasSummaryReport = ckSummaryReport.Checked;
            eRole.HasViewBusinessRules = ckViewBusinessRules.Checked;
            eRole.HasViewContact = ckViewContact.Checked;
            eRole.HasViewContactGroup = ckViewContactGroup.Checked;
            eRole.HasViewTemplate = ckViewLetterTemplate.Checked;
            eRole.HasViewUsersAvailableToCall = ckViewAvailableToCallUsers.Checked;
            eRole.HasUserLog = ckUserLog.Checked;
            eRole.HasWelcomeTemplate = ckWelcomeTemplate.Checked;
            eRole.HasMailSetting = ckMailSettings.Checked;

            //cRoles.GetMulti(null);
            //int index;
            //bool bExist = false;
            //for (index = 0; index < cRoles.Count; index++)
            //{
            //    if (cRoles[index].Name == txtName.Text)
            //    {
            //        bExist = true;
            //        break;
            //    }
            //}
            if (index == 0)
            {
                eRole.Save();
                lblSavedRole.Visible = true;
                //Response.Redirect("RoleList.aspx");
            }
            else
            {
                lblSteluta.Visible = true;
                lblGrupExist.Visible = true;
            }

            
        }
        catch { };      
    }
    protected void Cancel(object sender, EventArgs e)
    {
        Response.Redirect("RoleList.aspx");
    }   
}
